import 'package:flutter_jd/tools/data/mock.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get/state_manager.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class FindController extends GetxController {
  RefreshController refreshController = RefreshController(
    initialRefresh: false,
  );
  final productList = [].obs;
  int currentPage = 1;
  int lastPage = 5;

  @override
  void onInit() {
    super.onInit();
    loadData();
  }

  // 加载数据
  loadData({bool initData = false}) {
    currentPage++;
    productList.addAll(PRODUCT_LIST);
    update();
    refreshController.loadComplete();
  }

  // 下拉刷新
  onRefresh() async {
    loadData(initData: true);

    await Future.delayed(Duration(milliseconds: 600));
    refreshController.refreshCompleted();
  }

  // 加载更多
  onLoading() async {
    await Future.delayed(Duration(milliseconds: 200));
    refreshController.loadNoData();

    if (currentPage < lastPage) {
      loadData();
    } else if (currentPage == lastPage) {
      refreshController.loadNoData();
    }
  }
}
